package com.apexedu.biz.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.biz.service.AjaxBizService;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.ConfigUtil;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.sys.SysConstant;
import com.apexedu.sys.service.impl.AjaxSysServiceImpl;
import com.apexedu.util.StringUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 提供AJAX支持的类，返回字符串结果，可以是JSON，客户端用AJAX直接调用。
 *
 * @author Linyang datetime 2011-5-1
 */
public class AjaxBizServiceImpl extends AjaxSysServiceImpl implements AjaxBizService {

	private BaseDao baseDao;

	/*
	 * private BaseDao baseDao;
	 * public void setBaseDao(BaseDao baseDao) {
	 * this.baseDao = baseDao;
	 * }
	 */

	/**
	 * tree 加载子节点 菜单维护 在areaTree模块使用ajax调用
	 *
	 * @param treeType 数据来源类型：depart, area, menu ...
	 * @param nodeid 上级节点编码
	 * @return
	 */
	public String loadTreeChild(String treeType, String nodeid) {

		//String sql = null;
		if (null != treeType) {
			if (SysConstant.TREETYPE_REPORTCHILD.equals(treeType)) {
				return loadReportClassTreeChild(nodeid);
				//sql = SQLConfigUtil.getSql("sql.reportclass.loadMySons") ;
				//return loadTreeChild(sql, new Object[] { nodeid });
			} else if (Const.TreeType.AUC_MENU.equals(treeType)) {
				return loadMenuTreeChild(nodeid);
			}
		}
		return "{}";
	}

	private String loadMenuTreeChild(String nodeid) {
		String sql = " select menuid as id, menuname||'（'||a.ordernum||'）' as text," + " (select count(*) from t_sys_menu where upmenuid=a.menuid) as childCount, " + " replace(menupath,'''','') as value,upmenuid as parentid from T_Sys_Menu a"
				+ " left join t_access_app_reg t on a.belongtoapp=t.appid" + " where upmenuid=? order by a.ordernum desc";
		return loadTreeChild(sql, new Object[] { nodeid });
	}

	/**
	 * 报表分类子节点
	 *
	 * @param nodeid
	 * @return
	 */
	private String loadReportClassTreeChild(String nodeid) {
		String sql = "select wid as id,classname as text,(select count(*) from t_report_class where wid=a.parentwid) as childCount," + "parentwid as parentid from t_report_class a where state=1 and parentwid =? order by ordernum desc";
		return loadTreeChild(sql, new Object[] { nodeid });
	}

	/**
	 * 批量授权角色
	 *
	 * @param request
	 * @return
	 */
	public boolean empower(HttpServletRequest request) {
		String ids = request.getParameter("ids"); //用户id
		String roleid = request.getParameter("roleid"); //角色id
		if (StringUtil.isBlank(ids)) { //无用户id时，授权当前查询条件下所有用户
			String userRoleSql = (String) request.getSession().getAttribute("userRoleSql");
			if (StringUtil.isNotBlank(userRoleSql)) {
				String sql = "";
				sql += "insert into t_sys_user_role(id,userid,roleid) ";
				sql += "	select sys_guid(),a.userid,? ";
				sql += "	from t_sys_user a ";
				sql += userRoleSql;
				sql += " 		and not exists (select null from t_sys_user_role where roleid=? and userid=a.userid)";
				return DBUtil.executeSQL(sql, roleid, roleid) > 0;
			}
		} else {
			String sql = "";
			sql += "insert into t_sys_user_role(id,userid,roleid)";
			sql += "	select sys_guid(),a.userid,? ";
			sql += "	from t_sys_user a ";
			sql += "	where 1=1";
			sql += "		and a.userid in ('" + ids.replaceAll(",", "','") + "')";
			sql += "		and not exists (select null from t_sys_user_role where roleid=? and userid=a.userid)";

			return DBUtil.executeSQL(sql, roleid, roleid) > 0;
		}
		return false;
	}

	/**
	 * 批量删除角色
	 *
	 * @param request
	 * @return
	 */
	public boolean dodelete(HttpServletRequest request) {
		String ids = request.getParameter("ids"); //用户id
		String roleid = request.getParameter("roleid"); //角色id
		if (StringUtil.isBlank(ids)) { //全部删除
			return DBUtil.executeSQL("delete from t_sys_user_role where roleid = ?", roleid) > 0;
		} else {
			return DBUtil.executeSQL("delete from t_sys_user_role where userid in ('" + ids.replaceAll(",", "','") + "') and roleid = ?", roleid) > 0;
		}
	}
}
